package com.qidian.mes.pro.service.impl;

import com.qidian.mes.pojo.Order;
import com.qidian.mes.pro.mapper.OrderMapper;
import com.qidian.mes.pro.service.OrderService;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author dqy
 * @Description
 * @Time 2024/4/25 16:14
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public QdResult saveOrder(Order order) {
        List<Order> orderList =orderMapper.selectByCode(order.getWoCode());
        if(orderList != null){
            for (Order o : orderList){
                if (o.getWoStatus() != 99){
                    return QdResult.duplicate("工单编码已存在");
                }
            }
        }
        orderList = orderMapper.selectByName(order.getWoName());
        if(orderList != null){
            for (Order o : orderList){
                if (o.getWoStatus() != 99){
                    return QdResult.duplicate("工单名称已存在");
                }
            }
        }
        return QdResult.isSuccess(orderMapper.insertOrder(order));
    }

    @Override
    public QdResult updateOrder(Order order) {
        if (order.getWoId() == null){
            return QdResult.error("未找到工单Id");
        }
        Order order1 = orderMapper.selectById(order.getWoId());
        if(order.getWoName().equals(order1.getWoName())&&order.getWoCode().equals(order1.getWoCode())){
            return QdResult.success(orderMapper.updateOrder(order));
        }
        List<Order> orderList =orderMapper.selectByCode(order.getWoCode());
        if (orderList != null){
            for(Order o : orderList){
                if (o.getWoStatus() != 99){
                    return QdResult.duplicate("工单编码已存在");
                }
            }
        }
        orderList = orderMapper.selectByName(order.getWoName());
        if (orderList != null){
            for(Order o : orderList){
                if (o.getWoStatus() != 99){
                    return QdResult.duplicate("工单名称已存在");
                }
            }
        }
        return QdResult.isSuccess(orderMapper.updateOrder(order));
    }

    @Override
    public QdResult deleteOrder(Long woId) {
        orderMapper.deleteOrder(woId);//删除父工单
        deleteChildrenOrders(woId);//删除子工单
        return QdResult.success();
    }
    public void deleteChildrenOrders(Long parentId) {
        List<Order> orders = orderMapper.selectByParentId(parentId);
        System.out.println(orders);
       if (orders != null && !orders.isEmpty()){
           for (Order o : orders){
              deleteChildrenOrders(o.getWoId());
               orderMapper.deleteOrder(o.getWoId());
           }
       }
    }

    @Override
    public QdResult deletes(Long[] woIds) {
        orderMapper.deleteOrders(woIds);
        return QdResult.success();
    }

    @Override
    public QdResult<PagerDTO<Order>> queryByPage(Order order, int start, int limit) {
        PagerDTO<Order> pagerDTO = new PagerDTO<>();
        pagerDTO.setPage(start);
        pagerDTO.setLimit(limit);
        pagerDTO.setTotal(orderMapper.selectCount(order));
        pagerDTO.setData(orderMapper.queryByPage(order,(start-1)*limit,limit));
        return QdResult.success(pagerDTO);
    }

    @Override
    public QdResult queryById(Long woId) {
        return QdResult.success(orderMapper.selectById(woId));
    }

    /**
     * 查询所有工单
     * @return
     */
    @Override
    public QdResult<List<Order>> queryAll() {
        List<Order> orders = orderMapper.selectByParentId(0L);
        OrderService orderService = (OrderService) AopContext.currentProxy();
        for (Order o : orders){
            List<Order> children = orderService.queryByParentId(o.getWoId());
            if (children != null && children.size()>0){
                o.setChildren(children);
            }
        }
        System.out.println("----------------------------------------------------------------------------------------------------------");
        System.out.println(orders);
        return QdResult.success(orders);
    }

    /**
     * 根据父id查询子工单
     * @param woParentId
     * @return
     */

    @Override
    public List<Order> queryByParentId(Long woParentId) {
        List<Order> orders = orderMapper.selectByParentId(woParentId);
        if(orders == null || orders.size() == 0){
            return null;
        }
        OrderService orderService = (OrderService) AopContext.currentProxy();
        for (Order o : orders){
            List<Order> children = orderService.queryByParentId(o.getWoId());
            o.setChildren(children);
        }
        return orders;
    }
}
